package com.xyd.Servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xyd.Service.OrderService;
import com.xyd.bean.Order;
import com.xyd.bean.User;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet({"/admin/order","/admin/addorder","/admin/orderDetail","/admin/orderModify",
        "/admin/orderDel","/manage/admin/searchOrder"})
public class AdminManageOrderServlet extends HttpServlet {
    OrderService orderService = new OrderService();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String path = request.getServletPath();
        if ("/admin/addorder".equals(path)) {
            doAddOrder(request, response);
        } else if ("/admin/order".equals(path)) {
            doOrder(request, response);
        } else if ("/admin/orderDetail".equals(path)) {
            doOrderDetail(request, response);
        } else if ("/admin/orderModify".equals(path)) {
            doModifyOrder(request, response);
        } else if ("/admin/orderDel".equals(path)) {
            doDel(request, response);
        }else if ("/manage/admin/searchOrder".equals(path)){
            doSearch(request,response);
        }
    }

    private void doSearch(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String username = request.getParameter("username");
        List<Order> orders = orderService.getByName(username);
        if (orders.size()>0){
            ObjectMapper om = new ObjectMapper();
            String json = om.writeValueAsString(orders);
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print(json);
        }
    }

    private void doModifyOrder (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            String id = request.getParameter("userid");
            int userid = Integer.parseInt(id);
            String username = request.getParameter("username");
            String useraddress = request.getParameter("useraddress");
            String cost = request.getParameter("cost");
            double ocost = Double.parseDouble(cost);
            String status = request.getParameter("status");
            int ostatus = Integer.parseInt(status);
            Order order = new Order(userid, username, useraddress, ocost, ostatus);
            int i = orderService.modefiyOrder(order);
            if (i > 0) {
                response.sendRedirect("/admin/order");
            }
        }

        private void doOrderDetail (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            String id = request.getParameter("id");
            Order order = orderService.getById(Integer.parseInt(id));
            if (order != null) {
                request.setAttribute("order", order);
                request.getRequestDispatcher("/manage/order-modify.jsp").forward(request, response);
            }
        }

        private void doOrder (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            List<Order> orders = orderService.getAll();
            if (orders.size() > 0) {
                request.getSession().setAttribute("orders", orders);
                response.sendRedirect("/manage/order.jsp");
            }else {
                response.sendRedirect("/manage/order.jsp");
            }
        }

        private void doAddOrder (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                User user = (User) request.getSession().getAttribute("user");
                Integer userid = user.getUid();
                String username = user.getUname();
                String address = user.getAddress();
                double coast = orderService.getCost(userid);
                Order order = new Order(userid, username, address, coast, 1);
                int i = orderService.addOrder(order);
                if (i > 0) {
                    response.getWriter().println("true");
                }

        }

    private void doDel(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("id");
        int i = orderService.delOrder(Integer.parseInt(id));
        if (i>0){
            response.sendRedirect("/admin/order");
        }

    }
}
